import axios       from 'axios'
import sign        from '@/utils/signature'
import { Message } from 'element-ui'

const Http = {

    get(url) {
        const { timestamp, nonce, signature } = sign.generateModulesApiSignature();

        return axios.get(url, {
            params: { timestamp, nonce, signature }
        }).then((res) => {
            if (res.data.code === 200) {
                return res;
            } else if (res.data.code === 403) {
                Message({
                    showClose: true,
                    message: '登陆已失效，请重新登陆！',
                    type: 'warning'
                });
                window.location = "/adminnew/#/";
            } else {
                Message({
                    showClose: true,
                    message: res.data.message,
                    type: 'error'
                });
                
                return Promise.reject(res);
            }
        })
    },
    post(url, data) {
        const { timestamp, nonce, signature } = sign.generateModulesApiSignature();
        Object.assign(data, { timestamp, nonce, signature });

        return axios.post(url, data).then((res) => {
            if (res.data.code === 200) {
                return res; //成功
            } else if (res.data.code === 403) {
                Message({
                    showClose: true,
                    message: '登陆已失效，请重新登陆！',
                    type: 'warning'
                });
                window.location = "/adminnew/#/";
            } else {
                Message({
                    showClose: true,
                    message: res.data.message,
                    type: 'error'
                });

                return Promise.reject(res);
            }
        })
    },

};

export default Http;
